<template>
  <div class="root">
    <div class="header">
      <el-button
        class="btn-add"
        @click="exportData(99999)"
        size="small"
        type="primary"
        round
        >数据导出</el-button
      >
    </div>
    <el-table ref="multipleTable" :data="tableData" style="width: 100%">
      <el-table-column
        prop="username"
        label="姓名"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="telephone"
        label="会员手机号"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="sex"
        label="性别"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="graduationDate"
        label="毕业时间"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="companyName"
        label="公司名"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="department"
        label="部门"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="position"
        label="职位"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="workAddress"
        label="工作地"
        show-overflow-tooltip
      ></el-table-column>
    </el-table>
    <el-pagination
      class="pagination"
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="params.pageSize"
      @current-change="pageChange"
      @prev-click="pageChange"
      @next-click="pageChange"
    ></el-pagination>
  </div>
</template>
<script>
import { DownLoadXLSX } from "../../utils/xlsx";
export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 10
      },
      total: 0,
      tableData: []
    };
  },
  created() {
    this.getTableData();
  },
  methods: {
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    getTableData() {
      let that = this;
      this.req({
        url: `/user/list?page=${this.params.page}&pageSize=${this.params.pageSize}`,
        data: {},
        method: "POST"
      }).then(
        res => {
          const { data } = res;
          that.total = data.total;
          that.tableData = data.list;
        },
        err => {
          console.log("err :", err);
        }
      );
    },
    pageChange(page) {
      console.log("page :", page);
      // this.getTemplateList(page);
    },
    deleteItem(id) {
      console.log("delete", id);
    },
    editItem(id) {
      console.log("edit", id);
      this.$router.push({ name: "addActivity", query: { activityId: id } });
    },
    addActivity() {
      this.$router.push({ name: "addActivity" });
    },
    openParticipant(id) {
      console.log("iddd");
      this.$router.push({ name: "participant", query: { activityId: id } });
    },
    async exportData(n = 20) {
      const { data } = await this.req({
        url: `/user/list?page=1&pageSize=${n}`,
        method: "POST"
      });
      const headers = [
        "姓名",
        "会员手机号",
        "性别",
        "毕业时间",
        "公司名",
        "部门",
        "职位",
        "工作地"
      ];
      const list = (data && data.list) || [];

      // 表格表头数据
      const SheetData = [headers];
      list.forEach(i => {
        const item = [
          i.username, // 姓名
          i.telephone, // 手机号
          i.sex, // 性别
          i.graduationDate, // 毕业时间
          i.companyName, // 公司名
          i.department, // 部门
          i.position, // 职位
          i.workAddress // 工作地
        ];
        SheetData.push(item);
      });

      DownLoadXLSX(SheetData, "成员信息表");
    }
  }
};
</script>
<style scoped>
.header {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 10px;
}
.input-with-select {
  width: 50%;
}
.pagination {
  margin-top: 20px;
  margin-right: 50px;
  float: right;
}
.btn-danger {
  color: red !important;
}
</style>
